﻿Imports System.Data.SqlClient
Public Class frmServicesFrn
    'Matériels
    Dim totalDette As Double
    Public Sub remplirlstViwSrv()
        lstViwSrv.Items.Clear()
        Cmd = New SqlCommand("select * from serviceFour where idFr = " & idFournGnlr, Con)
        Dr = Cmd.ExecuteReader
        If Dr.HasRows Then
            While Dr.Read
                Dim Line As ListViewItem = New ListViewItem(New String() {Dr.GetInt32(0), Dr.GetString(2), Dr.GetDouble(3), ""})
                lstViwSrv.Items.Add(Line)
            End While
        End If
        Dr.Close()
        totalDette = 0
        For Each it As ListViewItem In lstViwSrv.Items
            remplirLstPayementFourn(CInt(it.SubItems(0).Text))
            rest = Math.Round(CDbl(it.SubItems(2).Text), 2)
            For Each payParrai In lstPaiement
                rest -= Math.Round(payParrai.montant, 2)
            Next
            it.SubItems(3).Text = rest
            totalDette += rest
        Next
        lblDet.Text = "Total des dettes : " & totalDette
        txtPrix.Clear()
        rchTxtTache.Clear()
    End Sub
    
    'End
    Private Sub frmServicesFrn_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        remplirCmb(cmbFour, "select id_Fournisseur, nom + [Nom_entreprise] + prenom from Fournisseus where [TYPE] in ('Réparations','Réparations et Matériels')")
    End Sub

    Private Sub btnRechFour_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRechFour.Click
        Me.Enabled = False
        frmRechFour.Show()
    End Sub

    Public Sub cmbFour_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbFour.SelectedIndexChanged
        Try
            idFournGnlr = CType(cmbFour.SelectedItem, DictionaryEntry).Key
            open()
            remplirlstViwSrv()
            cnClose()
        Catch ex As Exception
            MsgBox("Une erreur c'est produite, veuillez informer l'administrateur de l'application !!!" & Environment.NewLine & "Déscription de l'erreur :" & Environment.NewLine & ex.Message, MsgBoxStyle.Critical, "Erreur")
        End Try
    End Sub

    Private Sub lstViwSrv_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstViwSrv.SelectedIndexChanged
        Try
            If lstViwSrv.SelectedItems.Count > 0 Then
                idSrv = CInt(lstViwSrv.SelectedItems(0).SubItems(0).Text)
                rchTxtTache.Text = lstViwSrv.SelectedItems(0).SubItems(1).Text
                txtPrix.Text = lstViwSrv.SelectedItems(0).SubItems(2).Text
            Else
                txtPrix.Clear()
                rchTxtTache.Clear()
            End If
        Catch ex As Exception
            MsgBox("Une erreur c'est produite, veuillez informer l'administrateur de l'application !!!" & Environment.NewLine & "Déscription de l'erreur :" & Environment.NewLine & ex.Message, MsgBoxStyle.Critical, "Erreur")
        End Try
    End Sub

    Private Sub txtPrix_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPrix.KeyPress
        Dim allowedChars As String = "1234567890,"
        If e.KeyChar = ChrW(8) Then
            e.Handled = False
        ElseIf allowedChars.IndexOf(e.KeyChar) = -1 Then
            ' Invalid Character
            e.Handled = True
        End If
    End Sub

    Private Sub btnAjouter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAjouter.Click
        Dim prix As Double = 0
        Dim isPrixEmpty As Boolean = False
        Dim prixVrai As Boolean = False
        Try
            If cmbFour.SelectedIndex >= 0 Then
                If Not String.IsNullOrEmpty(rchTxtTache.Text) Or Not String.IsNullOrWhiteSpace(rchTxtTache.Text) Then
                    'Rassuré le prix
                    If Not String.IsNullOrWhiteSpace(txtPrix.Text) And Not String.IsNullOrEmpty(txtPrix.Text) Then
                        Double.TryParse(txtPrix.Text, prix)
                    Else
                        isPrixEmpty = True
                    End If

                    If Not isPrixEmpty Then
                        If prix >= 0 Then
                            prixVrai = True
                        Else
                            MsgBox("Prix incorrecte!!!" & Environment.NewLine & "Vous avez entrez beaucoup de virgules", MsgBoxStyle.Critical, "Erreur")
                            Exit Sub
                        End If
                    End If
                    'End

                    If isPrixEmpty Or prixVrai Then
                        Cmd = New SqlCommand("INSERT INTO [serviceFour]([idFr],[taches],[prix]) " &
                                             "VALUES(" & idFournGnlr & ",@taches,@prix)", Con)
                        open()
                        Cmd.Parameters.Add("@prix", SqlDbType.Float)
                        Cmd.Parameters("@prix").Value = Math.Round(prix, 2)
                        Cmd.Parameters.Add("@taches", SqlDbType.VarChar)
                        Cmd.Parameters("@taches").Value = rchTxtTache.Text
                        Cmd.ExecuteNonQuery()

                        remplirlstViwSrv()
                        cnClose()
                        txtPrix.Clear()
                        rchTxtTache.Clear()

                        MsgBox("Le service est bien ajouté à ma liste", MsgBoxStyle.Information, "succès")
                    End If

                Else
                    MsgBox("Vous devez saisir les tâches", MsgBoxStyle.Critical, "Erreur")
                End If
            Else
                MsgBox("Vous devez séléctioner un fournisseur", MsgBoxStyle.Critical, "Erreur")
            End If
        Catch ex As Exception
            MsgBox("Une erreur c'est produite, veuillez informer l'administrateur de l'application !!!" & Environment.NewLine & "Déscription de l'erreur :" & Environment.NewLine & ex.Message, MsgBoxStyle.Critical, "Erreur")
        End Try
    End Sub

    Private Sub btnSupp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSupp.Click
        Try
            If lstViwSrv.SelectedItems.Count > 0 Then
                Cmd = New SqlCommand("DELETE FROM [serviceFour] WHERE idServ = " & idSrv, Con)
                open()
                Cmd.ExecuteNonQuery()
                cnClose()
                For Each lstIt As ListViewItem In lstViwSrv.Items
                    If CInt(lstIt.SubItems(0).Text) = idSrv Then
                        lstIt.Remove()
                        totalDette -= CDbl(lstIt.SubItems(3).Text)
                        lblDet.Text = "Total des dettes : " & totalDette
                        Exit For
                    End If
                Next
                MsgBox("Le service est bien supprimé de ma liste", MsgBoxStyle.Information, "succès")
                txtPrix.Clear()
                rchTxtTache.Clear()
            Else
                MsgBox("Veuillez sélectionner un service de la list", MsgBoxStyle.Critical, "Erreur")
            End If
        Catch ex As Exception
            MsgBox("Une erreur c'est produite, veuillez informer l'administrateur de l'application !!!" & Environment.NewLine & "Déscription de l'erreur :" & Environment.NewLine & ex.Message, MsgBoxStyle.Critical, "Erreur")
        End Try
    End Sub


    Private Sub btnPayer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPayer.Click
            Try
            If lstViwSrv.SelectedItems.Count > 0 Then
                If txtPrix.Text <> "0" And Not String.IsNullOrEmpty(txtPrix.Text) And Not String.IsNullOrWhiteSpace(txtPrix.Text) Then
                    somParrain = New sommeParain
                    With somParrain
                        .idParain = CInt(lstViwSrv.SelectedItems(0).SubItems(0).Text)
                        .somme = lstViwSrv.SelectedItems(0).SubItems(2).Text
                    End With
                    rest = CDbl(lstViwSrv.SelectedItems(0).SubItems(3).Text)
                    Me.Enabled = False
                    frmPaiementFour.Show()
                Else
                    MsgBox("Ce service est gratuit!!!", MsgBoxStyle.Critical, "Erreur")
                End If
            Else
                MsgBox("Veuillez sélectionner un service de la list", MsgBoxStyle.Critical, "Erreur")
            End If
        Catch ex As Exception
            MsgBox("Une erreur c'est produite, veuillez informer l'administrateur de l'application !!!" & Environment.NewLine & "Déscription de l'erreur :" & Environment.NewLine & ex.Message, MsgBoxStyle.Critical, "Erreur")
        End Try
       
    End Sub
End Class